package com.snowfield.lbcommon.utils.commonsexec;

import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author snowfield
 */
public class ExecuteResultHandler implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExecuteResultHandler.class);

    DefaultExecuteResultHandler handler;

    public ExecuteResultHandler(DefaultExecuteResultHandler handler) {
        this.handler = handler;
    }

    public void run() {
        while (!handler.hasResult()) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                LOGGER.error("", e);
            }
        }
        /**
         * 处理异常
         */
        Exception e = handler.getException();
        if (e != null) {
            LOGGER.error("", e);
        }
        /**
         * 处理执行结果
         */
        int exitValue = handler.getExitValue();
        LOGGER.info("exitValue : {}", exitValue);
    }

}
